
package com.ifcar.app.util;

import org.json.JSONArray;
import org.json.JSONObject;

import android.text.TextUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class ModelUtils {

    /**
     * map 转 json
     * 
     * @param map
     * @return
     */
    public static JSONObject map2Json(Map<String, Object> map) {
        JSONObject json = new JSONObject();
        if (map != null) {
            Iterator<String> iterator = map.keySet().iterator();
            while (iterator.hasNext()) {
                try {
                    String key = iterator.next();

                    if (map.containsKey(key)) {

                        Object o = map.get(key);
                        if (o == null) {
                            continue;
                        }

                        if (o instanceof Map) {
                            json.put(key, map2Json((Map<String, Object>) o));
                        } else if (o instanceof List) {
                            List list = (List) o;
                            final int length = list.size();
                            JSONArray array = new JSONArray();

                            for (int i = 0; i < length; i++) {
                                array.put(map2Json((Map<String, Object>) list.get(i)));
                            }

                            json.put(key, array);
                        } else if (o instanceof String) {
                            json.put(key, Tools.htmlDecoder((String) o));
                        } else {
                            json.put(key, o);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }
        return json;
    }

    /**
     * json 转 map
     * 
     * @param json
     * @return
     */
    public static Map<String, Object> json2Map(JSONObject json) {
        Map<String, Object> map = new HashMap<String, Object>();

        if (json != null) {
            Iterator<String> iterator = json.keys();

            while (iterator.hasNext()) {
                try {
                    String key = iterator.next();

                    if (!json.isNull(key)) {

                        Object o = json.get(key);
                        if (o == null) {
                            continue;
                        }

                        if (o instanceof JSONObject) {
                            map.put(key, json2Map((JSONObject) o));
                        } else if (o instanceof JSONArray) {
                            JSONArray array = (JSONArray) o;
                            final int length = array.length();
                            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
                            for (int i = 0; i < length; i++) {
                                list.add(json2Map(array.getJSONObject(i)));
                            }

                            map.put(key, list);
                        } else if (o instanceof String) {
                            map.put(key, Tools.htmlDecoder((String) o));
                        } else {
                            map.put(key, o);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }

        return map;
    }

    /**
     * 返回map中值<br/>
     * 
     * @param map
     * @param key
     * @return
     */
    public static Object getValue(Map<String, Object> map, String key) {
        if (map == null || TextUtils.isEmpty(key)) {
            return null;
        }
        return map.get(key);
    }
}
